# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=tcl
pkgver=8.6.15
pkgrel=0
_major=${pkgver%.*}
pkgdesc="The Tcl scripting language"
url="https://www.tcl-lang.org/"
arch="all"
license="TCL"
depends="tzdata"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="zlib-dev sqlite-dev"
subpackages="$pkgname-doc $pkgname-dev"
source="https://downloads.sourceforge.net/sourceforge/tcl/tcl$pkgver-src.tar.gz
	tcl-stat64.patch
	"
builddir="$srcdir/tcl$pkgver/unix"

prepare() {
	cd "$srcdir"/tcl$pkgver
	for i in $source; do
		case $i in
			*.patch) msg "$i"; patch -p1 -i "$srcdir"/$i ;;
		esac
	done
	rm -r pkgs/sqlite3*
}

build() {
	local _64bit="--disable-64bit"
	case "$CARCH" in
		x86_64) _64bit="--enable-64bit";;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--with-system-sqlite \
		$_64bit
	make
}

package() {
	make INSTALL_ROOT="$pkgdir" install install-private-headers

	ln -sf tclsh$_major "$pkgdir"/usr/bin/tclsh
	install -Dm644 ../license.terms \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# remove buildroot traces
	find "$pkgdir" -name '*Config.sh' -print0 |
		xargs -0 sed -i -e "s#${srcdir}#/usr/src#"
}

check() {
	# thread-4.5 and thread-5* is suppressed because it tests UB
	# http* is suppressed because it tries to do network i/o
	# unixInit-3* is suppressed due to hardcoded locale assumptions
	make test TESTFLAGS="-verbose pbtels -skip 'thread-4.5 thread-5* http* unixInit-3*'"
}

dev() {
	default_dev

	local file; find "$pkgdir" -name '*Config.sh' | while read -r file; do
		amove "${file#"$pkgdir"}"
	done
}

sha512sums="
9ae652823084899091467744da5a35d0fdfb453c055baea96af1bb181d161abe58b83382315cc3abee5fd57acc4ad5028df486a3e53645a28d1467e9c8d1d23e  tcl8.6.15-src.tar.gz
cd52cbe80fd2be227c9018dbe38fa0888302293402e7a57f2e231c195b7c1902f1b58bc87d19c9d123915ca757e871abf084c0ef23b1a7f187bc03ba93904cc2  tcl-stat64.patch
"
